package com.summer.mall.controller.admin;

import com.summer.mall.common.Const;
import com.summer.mall.common.ResponseCode;
import com.summer.mall.common.ResponseService;
import com.summer.mall.entity.User;
import com.summer.mall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/manage/user")
public class UserManageController {
    @Autowired
    private UserService userService;

    /**
     * 用户登录功能接口
     * @param username
     * @param password
     * @param session
     * @return
     */

    @PostMapping(value = "/login")
    @ResponseBody
    public ResponseService<User> login(@RequestParam String username, @RequestParam String password, HttpSession session) {
        ResponseService<User> response = userService.login(username, password);

        if (response.isSuccess()) {
            if (response.getData().getRole() == Const.Role.ROLE_ADMIN) {
                session.setAttribute(Const.CURRENT_USER, response.getData());
                return response;
            } else {
                return ResponseService.createByErrorMessage("不是管理员无法登录");
            }
        }
        return response;
    }

    /**
     * 获取用户列表接口
     * @param session
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/list")
    @ResponseBody
    public ResponseService getList(HttpSession session, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        User currentUser = (User) session.getAttribute(Const.CURRENT_USER);
        if (currentUser == null) {
            return ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        //校验是不是管理员用户
        if (userService.checkAdminRole(currentUser).isSuccess()) {
            return userService.getUserList(pageNum, pageSize);
        } else {
            return userService.checkAdminRole(currentUser);
        }
    }

    /**
     * 修改用户的角色接口
     * @param session
     * @param userid
     * @param role
     * @return
     */
    @PostMapping(value = "/set_user_role")
    @ResponseBody
    public ResponseService<String> setUserRole(HttpSession session, Integer userid, Integer role) {
        User currentUser = (User) session.getAttribute(Const.CURRENT_USER);
        if (currentUser == null) {
            return ResponseService.createByCodeErrorMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        //校验是不是管理员用户
        if (userService.checkAdminRole(currentUser).isSuccess()) {
            return userService.setUserRole(userid, role);
        } else {
            return userService.checkAdminRole(currentUser);
        }
    }

    /**
     * 用户退出接口
     * @param session
     * @return
     */
    @PostMapping(value = "/logout")
    @ResponseBody
    public ResponseService<String> logout(HttpSession session) {
        session.removeAttribute(Const.CURRENT_USER);
        return ResponseService.createBySuccess("退出成功");
    }
}